//index.js
//获取应用实例
const app = getApp()

Page({
  
  
  data: {
    motto: '开始预测',
    shareText:'分享朋友',
    userInfo: {},
    hasUserInfo: false,
    welcome:'陌生人，你已进入了欣月塔罗屋。这里拥有神秘的力量。',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    ready:',准备好开始神秘之旅了吗?',
    isShare:false

  },


  //事件处理函数
  bindViewTap: function() {
    var that = this
  
    wx.navigateTo({
      url: '../selectCard/selectCard'
    })

    that.transferStatus(true)
 
  },
  bindShare:function(){
    var that = this

    wx.showShareMenu({
      withShareTicket:true,
      menus: ['shareAppMessage'],
      success(res){
        console.log('分享成功')
        that.transferStatus(true)
      }
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    this.transferStatus(false)
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  transferStatus:function(isSet){
    var that = this
    wx.getStorage({
      key: getCurrentDate(new Date()),
      success (res) {
        if(isSet){
          ++res.data;
        }
        if(res.data % 4 == 0){
          that.setData(
            {isShare:false}
          )
          console.log("可以预测"+res.data +that.data.isShare+isSet)
        }else{
          that.setData(
            {isShare:true}
          )
          console.log('不能预测'+res.data + that.data.isShare+isSet)
        }
        if(isSet){
          that.setShareCount(res.data)
        }
        
      },
      fail(res){
        wx.clearStorage({
          success: (res) => {
            console.log(res)
          },
        })

        that.setShareCount(0)
        console.log(res)
      }
    })

  },
  setShareCount:function(count){
    console.log(count)
    wx.setStorage({
      key: getCurrentDate(new Date()),
      data: count
    })
  }
})


function getCurrentDate(date){
  return date.getFullYear()+'-'+ (date.getMonth()+1)+'-'+date.getDate()
}


